package com.xinsmartsky.penmaji.penmaji;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.friendlyarm.AndroidSDK.HardwareControler;

import static java.lang.Thread.sleep;

public class LoginActivity extends Activity implements View.OnClickListener {
    private Button login_btn_edit = null;
    private Button login_btn_print = null;
    private Button login_btn_xxlb = null;//信息列表
    private View login_line_root;
    private PopupWindow popupWindow;
    private int replysig;//应答信号
    private int differ = 0;
    private SerialPortActivity serialport = new SerialPortActivity();
    private ProgressDialog mProgressDialog = null;
    private boolean flag = true;
    private int devfd = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        devfd = getIntent().getIntExtra("fd", 0);
        FindViewById();
        setOnClickListener();
        initPopupWindow();
        show();
    }

    /*onCreate被调用时 窗口连接是异步过程 （界面与窗口管理服务的双向通信连接尚未建立），
     可以将界面组件通过view.post函数发送过来的消息放入一个静态队列中
    * 通信连接建立完后再从该队列中读取消息并执行*/
    public void show() {
        login_line_root.post(new Runnable() {
            @Override
            public void run() {
                if (popupWindow != null) {
                    popupWindow.showAtLocation(login_line_root, Gravity.BOTTOM, 0, 0);
                }
            }
        });
    }

    private void FindViewById() {
        login_btn_edit = (Button) findViewById(R.id.login_btn_edit);
        login_btn_print = (Button) findViewById(R.id.login_btn_print);
        login_line_root = findViewById(R.id.login_line_root);
        login_btn_xxlb = (Button) findViewById(R.id.login_btn_xxlb);
    }

    private void setOnClickListener() {
        login_btn_edit.setOnClickListener(this);
        login_btn_print.setOnClickListener(this);
        login_btn_xxlb.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.login_btn_edit: {
                Bundle bundle = new Bundle();
                bundle.putInt("editfd", devfd);
                intent.putExtras(bundle);
                intent.setClass(LoginActivity.this, EditActivity.class);
                startActivity(intent);
                break;
            }
            //信息列表
//            case R.id.login_btn_xxlb: {
//                Bundle bundle = new Bundle();
//                bundle.putInt("xxlbfd", devfd);
//                intent.putExtras(bundle);
//                intent.setClass(LoginActivity.this, XXLBActivity.class);//没有设计界面
//                startActivity(intent);
//                break;
//            }
            case R.id.login_btn_print: {
                Bundle bundle = new Bundle();
                bundle.putInt("printfd", devfd);
                intent.putExtras(bundle);
                intent.setClass(LoginActivity.this, PrintSetActivity.class);
                startActivity(intent);
                break;
            }
        }
    }

    private void CreatDialogSmall() {
        Log.i("fd", "aaaaaa");
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("清洗中请稍候...");
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(true);
        mProgressDialog.show();
    }

    //是否向喷头供应墨水
    private void initPopupWindow() {
        View contentView = getLayoutInflater().inflate(R.layout.activity_login_menu, null);
        Button login_btn_yes = (Button) contentView.findViewById(R.id.login_btn_yes);
        Button login_btn_no = (Button) contentView.findViewById(R.id.login_btn_no);
       /*是否向喷头供应墨水 ：“是”*/
        login_btn_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //弹出断墨清洗浮窗
                CreatDialog();
            }
        });
         /*是否向喷头供应墨水 ：“否”*/
        login_btn_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.popupanimation);
    }

    //断墨清洗窗口
    private void CreatDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
        builder.setTitle("是否进行断墨清洗？");
        //”是“-->清洗键
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                serialport.CommuniProcess(devfd, (byte) 0xf2);
//                CreatDialogSmall();
            }//“否”--->上墨键
        }).setNegativeButton("否（直接上墨）", new DialogInterface.OnClickListener() {
            //"否"-->跳到Login2 并 向喷头供应墨水
            @Override
            public void onClick(DialogInterface dialog, int which) {
//
//                if () {//供墨成功
                Bundle bundle = new Bundle();
                Intent intent = new Intent();
                bundle.putInt("fd", devfd);
                bundle.putString("commend", "shangmo");
                intent.putExtras(bundle);
                intent.setClass(LoginActivity.this, LoginActivity2.class);
                startActivity(intent);
//                } else {//供墨失败
//                Bundle bundle = new Bundle();
//                Intent intent = new Intent();
//                bundle.putInt("fd", devfd);
//                bundle.putString("status", "failing");
//                intent.putExtras(bundle);
//                intent.setClass(LoginActivity.this, LoginActivity2.class);
//                startActivity(intent);
//                }
            }
        });
        builder.show();
    }


    private int BUFSIZE = 1;
    private byte[] receivebuf = new byte[BUFSIZE];

    /*写数据*/
    private int WriteThread(byte commend) {
        byte[] wtebyte = new byte[]{commend};
        int wrt = HardwareControler.write(devfd, wtebyte);
        return wrt;
    }

}



